package com.zzl.car.util;

import com.alibaba.fastjson.JSON;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.hash.Jackson2HashMapper;
import org.springframework.stereotype.Component;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class ZdpGetIllegal {


    public static Map getIllegal(String carPlate,String carEngine) throws IOException {
        StringBuffer url =new StringBuffer("https://fj.122.gov.cn/captcha?nocache=");
        long ticks = new Date().getTime();
        url.append(ticks);
        System.out.println(url);
        URL localUrl = new URL(url.toString());
        //获取到连接对象
        HttpURLConnection connection =(HttpURLConnection) localUrl.openConnection();
        File image = new File("demo.jpeg");
        connection.setRequestMethod("GET");
        //发送请求
        connection.connect();

        //获取到响应输入流
        InputStream inputStream = connection.getInputStream();
        //获取图片的输出流
        FileOutputStream imageOutputStream = new FileOutputStream(image);
        //读取输入流中的数据到图片中
        byte[] bytes = new byte[1024];
        int len = 0;
        while(( len=inputStream.read(bytes) )!=-1){
            imageOutputStream.write(bytes,0,len);
        }
        inputStream.close();
        imageOutputStream.close();
        //获取一下他们的cookie
        Map<String, List<String>> maps = connection.getHeaderFields();
        StringBuffer cookie = new StringBuffer();
        List<String> strings = maps.get("Set-Cookie");
        if(strings!=null){
            for(int i=0;i<strings.size();i++){
                String s = strings.get(i);
                if(s.startsWith("JSESSIONID-L")||
                        s.startsWith("TS018ff56c")){
                    if(cookie.length()==0){
                        cookie.append( s.split(";")[0]);
                    }else{
                        cookie.append(";"+s.split(";")[0]);
                    }
                }
            }
        }
        System.out.println(cookie);
        //识别验证码
        ITesseract instance = new Tesseract();
        //打开图片
        File imageFile = new File("demo.jpeg");
        //设置语言包文件夹
        instance.setDatapath("C:\\Program Files\\Tesseract-OCR\\tessdata");
        instance.setPageSegMode(7);
        String result = null;
        try{
            result = instance.doOCR(imageFile);
            System.out.println(result);
        } catch (TesseractException e) {
            e.printStackTrace();
        }
        localUrl = new URL("https://fj.122.gov.cn/m/publicquery/vio");
        HttpURLConnection httpURLConnection =(HttpURLConnection) localUrl.openConnection();
        httpURLConnection.setRequestMethod("POST");
        //生成参数
        String hphm1 =  carPlate.substring(1);
        String param = "hpzl=02&hphm1b="+hphm1+"&hphm="+carPlate+"&fdjh="+carEngine+"&captcha="+result+"&qm=wf&page=1";
        //因为post请求 参数是放在http正文中
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setUseCaches(false);
        httpURLConnection.setRequestProperty("Origin","https://fj.122.gov.cn");
        httpURLConnection.setRequestProperty("Referer","https://fj.122.gov.cn/views/inquiry.html");
        httpURLConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
        httpURLConnection.setRequestProperty("Cookie",cookie.toString());
        //将参数写到正文中
        OutputStream outputStream = httpURLConnection.getOutputStream();
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
        outputStreamWriter.write(param);
        outputStreamWriter.flush();

        outputStreamWriter.close();
        outputStream.close();
        StringBuffer result1 = new StringBuffer();
        //获取响应的数据
        String templine = null;
        inputStream = httpURLConnection.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        while(( templine=bufferedReader.readLine() )!=null){
            result1.append(templine);
        }
        inputStream.close();
        inputStreamReader.close();
        bufferedReader.close();
        Map<String,Object> map = JSON.parseObject(result1.toString(), Map.class);
        System.out.println(map);
        return map;
    }
}
